package com.youlu.campus.common.model;


import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;


/**
 * 统一返回结果RestRestResult.
 * <p>
 * Created by zhuhuaiqi
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestResult<T extends Object> {

    /**
     * 返回编码
     */
    @JSONField(ordinal = 1)
    private int code;

    /**
     * 返回消息
     */
    @JSONField(ordinal = 2)
    private String message;

    /**
     * 返回数据
     */
    @JSONField(ordinal = 3)
    private T data;

    public RestResult() {

    }

    public RestResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public RestResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static <T> RestResult<T> success() {
        GlobalErrorCode globalErrorCode = GlobalErrorCode.SUCCESS;
        RestResult<T> r = new RestResult<T>();
        r.setCode(globalErrorCode.getCode());
        r.setMessage(globalErrorCode.getMessage());
        return r;
    }

    public static <T> RestResult<T> success(T data) {
        GlobalErrorCode globalErrorCode = GlobalErrorCode.SUCCESS;
        RestResult<T> r = new RestResult<T>();
        r.setCode(globalErrorCode.getCode());
        r.setMessage(globalErrorCode.getMessage());
        r.setData(data);
        return r;
    }

    public static <T> RestResult<T> error() {
        GlobalErrorCode globalErrorCode = GlobalErrorCode.FAIL;
        RestResult<T> r = new RestResult<T>();
        r.setCode(globalErrorCode.getCode());
        r.setMessage(globalErrorCode.getMessage());
        return r;
    }

    public static <T> RestResult<T> error(GlobalErrorCode globalErrorCode) {
        RestResult<T> r = new RestResult<T>();
        r.setCode(globalErrorCode.getCode());
        r.setMessage(globalErrorCode.getMessage());
        return r;
    }

    public static <T> RestResult<T> error(String message) {
        GlobalErrorCode globalErrorCode = GlobalErrorCode.FAIL;
        RestResult<T> r = new RestResult<T>();
        r.setCode(globalErrorCode.getCode());
        r.setMessage(message);
        return r;
    }

    public static <T> RestResult<T> error(int code, String message) {
        RestResult<T> r = new RestResult<T>();
        r.setCode(code);
        r.setMessage(message);
        return r;
    }


}
